#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

//https://leetcode.cn/problems/fan-zhuan-dan-ci-shun-xu-lcof/solutions/804817/yi-ge-mo-ban-shua-bian-suo-you-zi-fu-chu-x6vh/
//https://leetcode.cn/problem-list/ex0k24j/
//字符串的套路题！！！
class Solution {
public:
    string reverseMessage(string message) {
        string temp = "";  //临时字符串
        vector<string> res; //存放字符串的数组
        for (char ch : message)  //遍历字符句子
        {
            if (ch == ' ') //遇到空格
            {
                if (!temp.empty()) //临时字符串非空
                {
                    res.push_back(temp);
                    temp.clear();  //清空临时字符串
                }
            }
            else
                temp += ch; 
        }
        if(!temp.empty()){
            res.push_back(temp);
        }
        std::string res_str="";
        for(auto item:res){
            res_str=item+" "+res_str;
        }
        if(!res_str.empty()){
            res_str.pop_back();
        }
        return res_str;

    }
};
class Solution {
public:
    string reverseWords(string s) {
        std::string res;
        std::string word;

        for(auto item:s){
            if(item==' '){
                if(!word.empty()){
                    std::reverse(word.begin(),word.end());
                    res=res+word;
                    word.clear();
                }
                res=res+item;
            }else{
                word=word+item;
            }
        }
        if(!word.empty()){
            std::reverse(word.begin(),word.end());
            res=res+word;
        }
        return res;
    }
};

